import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentMenu: null,
    tabsList: [
      { path: '/', name: 'home', label: '首页' }
    ]
  },
  getters: {},
  mutations: {
    selectMenu (state, val) {
      if (val.name === 'home') {
        state.currentMenu = null
      } else {
        state.currentMenu = val
        const isExit = state.tabsList.some(item => item.name === val.name)
        if (!isExit) {
          state.tabsList.push({ ...val })
        }
      }
    },

    // 关闭标签
    closeTab (state, val) {
      const result = state.tabsList.findIndex(item => item.name === val.name)
      state.tabsList.splice(result, 1)
    },

    // 改变当前路由
    changeRoute (state, val) {
      state.currentMenu = val
    },

    // 删除
    deleteFn (state, val) {
      if (val[1] === 'Left') {
        state.tabsList.splice(1, val[0] - 1)
      } else if (val[1] === 'Right') {
        state.tabsList.splice(val[0] + 1)
      } else if (val[1] === 'Others') {
        state.tabsList = val[0]
      } else if (val === 'All') {
        state.tabsList.splice(1)
      }
    }

  },
  actions: {},
  modules: {}
})
